<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require_once("entity.class.php");

class message extends entity {

	var $id;
	var $userid;
	var $message;
	var $from;
	var $sent;

	public function __construct()
   {
   }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		return new message;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".MESSAGES." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function listAll() {
		$sql = "SELECT * FROM ".MESSAGES." WHERE userid = ".util::str($this->userid)." ORDER BY sent DESC";
		return $sql;
	}

	public function insert() {
		$sql = "INSERT INTO ".MESSAGES." (userid, message, from, sent) ";
		$sql .= "VALUES (".util::str($this->userid).",".util::str(util::encode($this->message)).",".util::str($this->from).",".util::str(util::now()).")";
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".MESSAGES;
		$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function deleteAll($l) {
		$sql = "";
		foreach ($l as $item) {
			if ($sql!="") $sql.=" OR ";
			$sql.="id = ".util::str($item->id);
		}
		return "DELETE FROM ".MESSAGES." WHERE ".$sql;
	}
	
	public function fill($array) {
		$this->id=$array['id'];
		$this->userid=$array['userid'];
		$this->message=util::decode($array['message']);
		$this->from=$array['from'];
		$this->sent=$array['sent'];
	}

}

?>
